import React, { Component } from 'react'
import PureRenderMixin from 'react-addons-pure-render-mixin'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'

import * as userInfoActions from '../actions/userInfoAction'
import { CITYNAME } from '../config/localStorageKeys'
import localStore from '../util/localStore'

class App extends Component {
  constructor(props) {
    super(props);
    this.shouldComponentUpdate = PureRenderMixin.shouldComponentUpdate.bind(this);
    this.state = {
      initDone: false
    }
  }

  render() {
    return (
      <div>
        { this.state.initDone
          ? this.props.children
          : <p>加载中...</p>
        }
      </div>
    )
  }

  componentDidMount() {
    let cityName = localStore.getItem(CITYNAME);
    if(cityName == null) {
      cityName = '武汉';
    }

    this.props.userInfoActions.update({
      cityName: cityName
    })

    this.setState({
      initDone: true
    })
  }

}

const mapStateToProps = (state) => {
  return {}
}

const mapDispatchToProps = (dispatch) => {
  return {
    userInfoActions: bindActionCreators(userInfoActions, dispatch)
  }
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(App);
